package com.np.mvvmdemo1;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;

import com.np.mvvmdemo1.databinding.ActivityMainBinding;

/**
 * https://www.jianshu.com/p/1f1efa160ca1
 */
public class MainActivity extends AppCompatActivity {

    private MainViewModel mViewModel;
    private ActivityMainBinding mBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);

        // 初始化 View Binding
        mBinding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(mBinding.getRoot());

        // 初始化 ViewModel
        mViewModel = new ViewModelProvider(this).get(MainViewModel.class);

        // 观察 ViewModel 中的 LiveData 对象
        mViewModel.getUsers().observe(this, users -> {
            for (User user : users) {
                mBinding.textView.append(user.getName()+"\n");
            }
        });
    }
}